package core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangsen5 on 2016/10/26.
 */
public class ControllerCaller {

    public static String callMethod(HttpServletRequest request, HttpServletResponse response,Class clazz, Method method)
    {
        Object o = null;
        try {
             o=clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        for(Field f: clazz.getDeclaredFields())
        {
            try {
                if(f.getType().equals(HttpServletRequest.class))
                {
                    setObject(f,request,o);
                    continue;
                }
                if(f.getType().equals(HttpServletResponse.class))
                {
                    setObject(f,response,o);
                    continue;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            String p=request.getParameter(f.getName());
            if(null != p)
            {
                try {
                    setValue(f,p,o);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            Object re=method.invoke(o);
            if(method.getReturnType().equals(String.class))
            {
                return (String)re;
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;

    }
    private static void setObject(Field field,Object value,Object o) throws IllegalAccessException {
        field.setAccessible(true);
        field.set(o,value);
    }
    private static void setValue(Field field,String value,Object o) throws IllegalAccessException {

        field.setAccessible(true);

        if(field.getType().equals(String.class))
        {
            field.set(o,value);
            return;
        }
        if(field.getType().equals(Integer.class))
        {
            field.set(o,Integer.valueOf(value));
            return;
        }
        if(field.getType().equals(Long.class))
        {
            field.set(o,Long.valueOf(value));
            return;
        }
        if(field.getType().equals(Float.class))
        {
            field.set(o,Float.valueOf(value));
            return;
        }
        if(field.getType().equals(int.class))
        {
            field.setInt(o,Integer.valueOf(value).intValue());
            return;
        }
        if(field.getType().equals(boolean.class))
        {
            field.set(o,Boolean.valueOf(value).booleanValue());
            return;
        }
        if(field.getType().equals(Boolean.class))
        {
            field.setBoolean(o,Boolean.valueOf(value));
            return;
        }
        if(field.getType().equals(Double.class))
        {
            field.setDouble(o,Double.valueOf(value));
            return;
        }

    }

}
